package com.example.springapp.control;

import com.example.springapp.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller     // 接受前台的请求, 接受spring容器管理
public class LoginControl {

    //   127.0.0.1：8000/reg/
    @Autowired  //自动装备，从spring容器中获取对象
    private LoginService loginService;

    @RequestMapping(value = "/login")
    @ResponseBody       // 直接返回内容
    //   127.0.0.1：8000/login?code=zhang&pwd=123
    public String DoLogin(@RequestParam(value = "code",required = true) String code,
                          String pwd,
                          HttpSession session,
                          HttpServletRequest request){
        System.out.println(code + ", " + pwd + ", " + session.getId() + ", "+request);

        boolean bSuccess = loginService.login(code,pwd);
        if ( bSuccess ){
            return "success login";
        }else{
            return "login failed";
        }
    }

    @RequestMapping("/reg")
    @ResponseBody
    public String DoReg(){
        return "reg";
    }
}
